/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-01     Administrator       the first version
 */
#include <rtthread.h>
#include "board.h"
#include "pwm.h"
#include "uart.h"
#include "iic.h"
/**********************************************************************************/
/********************************  PWM_DEV_NAME ***********************************/
/*** timer3 pwm  *** timer4 pwm *** timer5 pwm ***  lpwm0 *** lpwm1 *** lpwm2 ***/
/*** t3pwm       *** t4pwm      *** t5pwm      ***  lpwm0 *** lpwm1 *** lpwm2 ***/
/**********************************************************************************/
   static struct rt_device_pwm *pwm_dev;      /* PWM设备句柄 */
void Pwm_Init(void){
    static rt_uint32_t period, pulse;
    period = 2140;    /* 周期 = 1M/period kHz */   // 470KHz
     pulse = period/2;          /* PWM脉冲宽度值(0 - period) */
    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
    RT_ASSERT(pwm_dev != RT_NULL);
    /* 设置PWM周期和脉冲宽度 */
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
    /* 使能设备 */
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);

       period =100000;    /* 周期 = 1M/period kHz */
       pulse = 10000000/(10000/35);          /* PWM脉冲宽度值(0 - period) */
      pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME0);
      RT_ASSERT(pwm_dev != RT_NULL);
      /* 设置PWM周期和脉冲宽度 */
      rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL0, period, pulse);
      /* 使能设备 */
      rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL0);

}
//
//
//            void pwm_out_adj(rt_uint32_t period, rt_uint32_t pulse)
//            {
//            //    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME0);
//                rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL0, period, pulse);
//            }


            void pwm_vout_adj(rt_uint32_t pulse,rt_bool_t sw)   //PWM 脉宽和开或关
            {
                pwm_data=pulse;
                 rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL0, 100000, 10000000/(10000/pulse)); //固定频率 //宽度化成百分比，*100更精确
                if(sw) rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL0);
                else rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL0);

                static uint16_t temp;
                if(sw)
                {
               switch( pwm_data) //档位
               {
               case 35: temp=0x04;TM1680_one();break;
               case 40: temp=0x05;TM1680_two();break;
               case 45: temp=0x06;TM1680_three();break;
               case 50: temp=0x07;TM1680_four();break;
               case 55: temp=0x08; TM1680_five();break;
               case 60: temp=0x09;TM1680_six();break;
               case 65: temp=0x0a;TM1680_seven();break;
               case 70: temp=0x0b; TM1680_eight();break;
               case 75: temp=0x0c; TM1680_nine();break;
               case 80: temp=0x0d;TM1680_ten();break;
               case 85: temp=0x0e; TM1680_eleven();break;
               case 90: temp=0x0f; TM1680_twelve();break;
               case 95: temp=0x10;TM1680_thirteen();break;
               case 10: temp=0x11;break;     //超温报警
               case 11: temp=0x12;break;     //过载报警
//               case 9:  temp=0xff;break;  //开机
//               case 8:  temp=0x00;break;  //关机
               default : ;break;
               }
                }
                else
                {
                    temp=0x14;  //关机
                    TM1680_logo();
                }
               uart_send_byte(temp);
            }


  void PWM_Thread_Entry(void *para)
  {
    Pwm_Init();

//    while(1){
//        /*使RGB灯红灯闪烁*/
//        for (int var = 0; var < period; var += 500) {
//            rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, var);
//            rt_thread_mdelay(50);
//        }
//    }
}

//#define PWM_DEV_NAME        "t5pwm"  /* PWM设备名称 */
//#define PWM_DEV_CHANNEL     1       /* PWM通道 */
//
//struct rt_device_pwm *pwm_dev;      /* PWM设备句柄 */
//
//  int PWM_Thread_Entry(int argc, char *argv[])
//{
//    rt_uint32_t period, pulse, dir;
//
//    period = 500000;    /* 周期为0.5ms，单位为纳秒ns */
//    dir = 1;            /* PWM脉冲宽度值的增减方向 */
//    pulse = 0;          /* PWM脉冲宽度值，单位为纳秒ns */
//
//    /* 查找设备 */
//    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
//    if (pwm_dev == RT_NULL)
//    {
//        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
//        return RT_ERROR;
//    }
//
//    /* 设置PWM周期和脉冲宽度默认值 */
//    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
//    /* 使能设备 */
//    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
//
//    while (1)
//    {
//        rt_thread_mdelay(50);
//        if (dir)
//        {
//            pulse += 5000;      /* 从0值开始每次增加5000ns */
//        }
//        else
//        {
//            pulse -= 5000;      /* 从最大值开始每次减少5000ns */
//        }
//        if (pulse >= period)
//        {
//            dir = 0;
//        }
//        if (0 == pulse)
//        {
//            dir = 1;
//        }
//
//        /* 设置PWM周期和脉冲宽度 */
//        rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
//    }
//}
///* 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(PWM_Thread_Entry, pwm sample);
